home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 401-425 / disk_419 / yacc / src.lzh / Src / output.c < prev    next >
C/C++ Source or Header  |  1990-07-14  |  20KB  |  1,140 lines

  1. #include "defs.h"
  2.  
  3. static int nvectors;
  4. static int nentries;
  5. static short **froms;
  6. static short **tos;
  7. static short *tally;
  8. static short *width;
  9. static short *state_count;
  10. static short *order;
  11. static short *base;
  12. static short *pos;
  13. static int maxtable;
  14. static short *table;
  15. static short *check;
  16. static int lowzero;
  17. static int high;
  18.  
  19.  
  20. output()
  21. {
  22.     free_itemsets();
  23.     free_shifts();
  24.     free_reductions();
  25.     output_stored_text();
  26.     output_defines();
  27.     output_rule_data();
  28.     output_yydefred();
  29.     output_actions();
  30.     free_parser();
  31.     output_debug();
  32.     output_stype();
  33.     write_section(header);
  34.     output_trailing_text();
  35.     write_section(body);
  36.     output_semantic_actions();
  37.     write_section(trailer);
  38. }
  39.  
  40.  
  41. output_rule_data()
  42. {
  43.     register int i;
  44.     register int j;
  45.  
  46.   
  47.     fprintf(output_file, "short yylhs[] = {%42d,",
  48.         symbol_value[start_symbol]);
  49.  
  50.     j = 10;
  51.     for (i = 3; i < nrules; i++)
  52.     {
  53.     if (j >= 10)
  54.     {
  55.         ++outline;
  56.         putc('\n', output_file);
  57.         j = 1;
  58.     }
  59.         else
  60.         ++j;
  61.  
  62.         fprintf(output_file, "%5d,", symbol_value[rlhs[i]]);
  63.     }
  64.     outline += 2;
  65.     fprintf(output_file, "\n};\n");
  66.  
  67.     fprintf(output_file, "short yylen[] = {%42d,", 2);
  68.  
  69.     j = 10;
  70.     for (i = 3; i < nrules; i++)
  71.     {
  72.     if (j >= 10)
  73.     {
  74.         ++outline;
  75.         putc('\n', output_file);
  76.         j = 1;
  77.     }
  78.     else
  79.       j++;
  80.  
  81.         fprintf(output_file, "%5d,", rrhs[i + 1] - rrhs[i] - 1);
  82.     }
  83.     outline += 2;
  84.     fprintf(output_file, "\n};\n");
  85. }
  86.  
  87.  
  88. output_yydefred()
  89. {
  90.     register int i, j;
  91.  
  92.     fprintf(output_file, "short yydefred[] = {%39d,",
  93.         (defred[0] ? defred[0] - 2 : 0));
  94.  
  95.     j = 10;
  96.     for (i = 1; i < nstates; i++)
  97.     {
  98.     if (j < 10)
  99.         ++j;
  100.     else
  101.     {
  102.         ++outline;
  103.         putc('\n', output_file);
  104.         j = 1;
  105.     }
  106.  
  107.     fprintf(output_file, "%5d,", (defred[i] ? defred[i] - 2 : 0));
  108.     }
  109.  
  110.     outline += 2;
  111.     fprintf(output_file, "\n};\n");
  112. }
  113.  
  114.  
  115. output_actions()
  116. {
  117.     nvectors = 2*nstates + nvars;
  118.  
  119.     froms = NEW2(nvectors, short *);
  120.     tos = NEW2(nvectors, short *);
  121.     tally = NEW2(nvectors, short);
  122.     width = NEW2(nvectors, short);
  123.  
  124.     token_actions();
  125.     FREE(lookaheads);
  126.     FREE(LA);
  127.     FREE(LAruleno);
  128.     FREE(accessing_symbol);
  129.  
  130.     goto_actions();
  131.     FREE(goto_map + ntokens);
  132.     FREE(from_state);
  133.     FREE(to_state);
  134.  
  135.     sort_actions();
  136.     pack_table();
  137.     output_base();
  138.     output_table();
  139.     output_check();
  140. }
  141.  
  142.  
  143. token_actions()
  144. {
  145.     register int i, j;
  146.     register int shiftcount, reducecount;
  147.     register int max, min;
  148.     register short *actionrow, *r, *s;
  149.     register action *p;
  150.  
  151.     actionrow = NEW2(2*ntokens, short);
  152.     for (i = 0; i < nstates; ++i)
  153.     {
  154.     if (parser[i])
  155.     {
  156.         for (j = 0; j < 2*ntokens; ++j)
  157.         actionrow[j] = 0;
  158.  
  159.         shiftcount = 0;
  160.         reducecount = 0;
  161.         for (p = parser[i]; p; p = p->next)
  162.         {
  163.         if (p->suppressed == 0)
  164.         {
  165.             if (p->action_code == SHIFT)
  166.             {
  167.             ++shiftcount;
  168.             actionrow[p->symbol] = p->number;
  169.             }
  170.             else if (p->action_code == REDUCE && p->number != defred[i])
  171.             {
  172.             ++reducecount;
  173.             actionrow[p->symbol + ntokens] = p->number;
  174.             }
  175.         }
  176.         }
  177.  
  178.         tally[i] = shiftcount;
  179.         tally[nstates+i] = reducecount;
  180.         width[i] = 0;
  181.         width[nstates+i] = 0;
  182.         if (shiftcount > 0)
  183.         {
  184.         froms[i] = r = NEW2(shiftcount, short);
  185.         tos[i] = s = NEW2(shiftcount, short);
  186.         min = MAXSHORT;
  187.         max = 0;
  188.         for (j = 0; j < ntokens; ++j)
  189.         {
  190.             if (actionrow[j])
  191.             {
  192.             if (min > symbol_value[j])
  193.                 min = symbol_value[j];
  194.             if (max < symbol_value[j])
  195.                 max = symbol_value[j];
  196.             *r++ = symbol_value[j];
  197.             *s++ = actionrow[j];
  198.             }
  199.         }
  200.         width[i] = max - min + 1;
  201.         }
  202.         if (reducecount > 0)
  203.         {
  204.         froms[nstates+i] = r = NEW2(reducecount, short);
  205.         tos[nstates+i] = s = NEW2(reducecount, short);
  206.         min = MAXSHORT;
  207.         max = 0;
  208.         for (j = 0; j < ntokens; ++j)
  209.         {
  210.             if (actionrow[ntokens+j])
  211.             {
  212.             if (min > symbol_value[j])
  213.                 min = symbol_value[j];
  214.             if (max < symbol_value[j])
  215.                 max = symbol_value[j];
  216.             *r++ = symbol_value[j];
  217.             *s++ = actionrow[ntokens+j] - 2;
  218.             }
  219.         }
  220.         width[nstates+i] = max - min + 1;
  221.         }
  222.     }
  223.     }
  224.     FREE(actionrow);
  225. }
  226.  
  227. goto_actions()
  228. {
  229.     register int i, j, k;
  230.  
  231.     state_count = NEW2(nstates, short);
  232.  
  233.     k = default_goto(start_symbol + 1);
  234.     fprintf(output_file, "short yydgoto[] = {%40d,", k);
  235.     save_column(start_symbol + 1, k);
  236.  
  237.     j = 10;
  238.     for (i = start_symbol + 2; i < nsyms; i++)
  239.     {
  240.     if (j >= 10)
  241.     {
  242.         ++outline;
  243.         putc('\n', output_file);
  244.         j = 1;
  245.     }
  246.     else
  247.         ++j;
  248.  
  249.     k = default_goto(i);
  250.     fprintf(output_file, "%5d,", k);
  251.     save_column(i, k);
  252.     }
  253.  
  254.     outline += 2;
  255.     fprintf(output_file, "\n};\n");
  256.     FREE(state_count);
  257. }
  258.  
  259. int
  260. default_goto(symbol)
  261. int symbol;
  262. {
  263.     register int i;
  264.     register int m;
  265.     register int n;
  266.     register int default_state;
  267.     register int max;
  268.  
  269.     m = goto_map[symbol];
  270.     n = goto_map[symbol + 1];
  271.  
  272.     if (m == n) return (0);
  273.  
  274.     for (i = 0; i < nstates; i++)
  275.     state_count[i] = 0;
  276.  
  277.     for (i = m; i < n; i++)
  278.     state_count[to_state[i]]++;
  279.  
  280.     max = 0;
  281.     default_state = 0;
  282.     for (i = 0; i < nstates; i++)
  283.     {
  284.     if (state_count[i] > max)
  285.     {
  286.         max = state_count[i];
  287.         default_state = i;
  288.     }
  289.     }
  290.  
  291.     return (default_state);
  292. }
  293.  
  294.  
  295.  
  296. save_column(symbol, default_state)
  297. int symbol;
  298. int default_state;
  299. {
  300.     register int i;
  301.     register int m;
  302.     register int n;
  303.     register short *sp;
  304.     register short *sp1;
  305.     register short *sp2;
  306.     register int count;
  307.     register int symno;
  308.  
  309.     m = goto_map[symbol];
  310.     n = goto_map[symbol + 1];
  311.  
  312.     count = 0;
  313.     for (i = m; i < n; i++)
  314.     {
  315.     if (to_state[i] != default_state)
  316.         ++count;
  317.     }
  318.     if (count == 0) return;
  319.  
  320.     symno = symbol_value[symbol] + 2*nstates;
  321.  
  322.     froms[symno] = sp1 = sp = NEW2(count, short);
  323.     tos[symno] = sp2 = NEW2(count, short);
  324.  
  325.     for (i = m; i < n; i++)
  326.     {
  327.     if (to_state[i] != default_state)
  328.     {
  329.         *sp1++ = from_state[i];
  330.         *sp2++ = to_state[i];
  331.     }
  332.     }
  333.  
  334.     tally[symno] = count;
  335.     width[symno] = sp1[-1] - sp[0] + 1;
  336. }
  337.  
  338. sort_actions()
  339. {
  340.   register int i;
  341.   register int j;
  342.   register int k;
  343.   register int t;
  344.   register int w;
  345.  
  346.   order = NEW2(nvectors, short);
  347.   nentries = 0;
  348.  
  349.   for (i = 0; i < nvectors; i++)
  350.     {
  351.       if (tally[i] > 0)
  352.     {
  353.       t = tally[i];
  354.       w = width[i];
  355.       j = nentries - 1;
  356.  
  357.       while (j >= 0 && (width[order[j]] < w))
  358.         j--;
  359.  
  360.       while (j >= 0 && (width[order[j]] == w) && (tally[order[j]] < t))
  361.         j--;
  362.  
  363.       for (k = nentries - 1; k > j; k--)
  364.         order[k + 1] = order[k];
  365.  
  366.       order[j + 1] = i;
  367.       nentries++;
  368.     }
  369.     }
  370. }
  371.  
  372.  
  373. pack_table()
  374. {
  375.     register int i;
  376.     register int place;
  377.     register int state;
  378.  
  379.     base = NEW2(nvectors, short);
  380.     pos = NEW2(nentries, short);
  381.  
  382.     maxtable = 1000;
  383.     table = NEW2(maxtable, short);
  384.     check = NEW2(maxtable, short);
  385.  
  386.     lowzero = 0;
  387.     high = 0;
  388.  
  389.     for (i = 0; i < maxtable; i++)
  390.     check[i] = -1;
  391.  
  392.     for (i = 0; i < nentries; i++)
  393.     {
  394.     state = matching_vector(i);
  395.  
  396.     if (state < 0)
  397.         place = pack_vector(i);
  398.     else
  399.         place = base[state];
  400.  
  401.     pos[i] = place;
  402.     base[order[i]] = place;
  403.     }
  404.  
  405.     for (i = 0; i < nvectors; i++)
  406.     {
  407.     if (froms[i])
  408.         FREE(froms[i]);
  409.     if (tos[i])
  410.         FREE(tos[i]);
  411.     }
  412.  
  413.     FREE(froms);
  414.     FREE(tos);
  415.     FREE(pos);
  416. }
  417.  
  418.  
  419. /*  The function matching_vector determines if the vector specified by    */
  420. /*  the input parameter matches a previously considered    vector.  The    */
  421. /*  test at the start of the function checks if the vector represents    */
  422. /*  a row of shifts over terminal symbols or a row of reductions, or a    */
  423. /*  column of shifts over a nonterminal symbol.  Berkeley Yacc does not    */
  424. /*  check if a column of shifts over a nonterminal symbols matches a    */
  425. /*  previously considered vector.  Because of the nature of LR parsing    */
  426. /*  tables, no two columns can match.  Therefore, the only possible    */
  427. /*  match would be between a row and a column.  Such matches are    */
  428. /*  unlikely.  Therefore, to save time, no attempt is made to see if a    */
  429. /*  column matches a previously considered vector.            */
  430. /*                                    */
  431. /*  Matching_vector is poorly designed.  The test could easily be made    */
  432. /*  faster.  Also, it depends on the vectors being in a specific    */
  433. /*  order.                                */
  434.  
  435. int
  436. matching_vector(vector)
  437. int vector;
  438. {
  439.     register int i;
  440.     register int j;
  441.     register int k;
  442.     register int t;
  443.     register int w;
  444.     register int match;
  445.     register int prev;
  446.  
  447.     i = order[vector];
  448.     if (i >= 2*nstates)
  449.     return (-1);
  450.  
  451.     t = tally[i];
  452.     w = width[i];
  453.  
  454.     for (prev = vector - 1; prev >= 0; prev--)
  455.     {
  456.     j = order[prev];
  457.     if (width[j] != w || tally[j] != t)
  458.         return (-1);
  459.  
  460.     match = 1;
  461.     for (k = 0; match && k < t; k++)
  462.     {
  463.         if (tos[j][k] != tos[i][k] || froms[j][k] != froms[i][k])
  464.         match = 0;
  465.     }
  466.  
  467.     if (match)
  468.         return (j);
  469.     }
  470.  
  471.     return (-1);
  472. }
  473.  
  474.  
  475.  
  476. int
  477. pack_vector(vector)
  478. int vector;
  479. {
  480.     register int i, j, k, l;
  481.     register int t;
  482.     register int loc;
  483.     register int ok;
  484.     register short *from;
  485.     register short *to;
  486.     int newmax;
  487.  
  488.     i = order[vector];
  489.     t = tally[i];
  490.     assert(t);
  491.  
  492.     from = froms[i];
  493.     to = tos[i];
  494.  
  495.     j = lowzero - from[0];
  496.     for (k = 1; k < t; ++k)
  497.     if (lowzero - from[k] > j)
  498.         j = lowzero - from[k];
  499.     for (;; ++j)
  500.     {
  501.     if (j == 0)
  502.         continue;
  503.     ok = 1;
  504.     for (k = 0; ok && k < t; k++)
  505.     {
  506.         loc = j + from[k];
  507.         if (loc >= maxtable)
  508.         {
  509.         if (loc >= MAXTABLE)
  510.             fatal("maximum table size exceeded");
  511.  
  512.         newmax = maxtable;
  513.         do { newmax += 200; } while (newmax <= loc);
  514.         table = (short *) REALLOC(table, newmax*sizeof(short));
  515.         if (table == 0) no_space();
  516.         check = (short *) REALLOC(check, newmax*sizeof(short));
  517.         if (check == 0) no_space();
  518.         for (l  = maxtable; l < newmax; ++l)
  519.         {
  520.             table[l] = 0;
  521.             check[l] = -1;
  522.         }
  523.         maxtable = newmax;
  524.         }
  525.  
  526.         if (check[loc] != -1)
  527.         ok = 0;
  528.     }
  529.     for (k = 0; ok && k < vector; k++)
  530.     {
  531.         if (pos[k] == j)
  532.         ok = 0;
  533.     }
  534.     if (ok)
  535.     {
  536.         for (k = 0; k < t; k++)
  537.         {
  538.         loc = j + from[k];
  539.         table[loc] = to[k];
  540.         check[loc] = from[k];
  541.         if (loc > high) high = loc;
  542.         }
  543.  
  544.         while (check[lowzero] != -1)
  545.         ++lowzero;
  546.  
  547.         return (j);
  548.     }
  549.     }
  550. }
  551.  
  552.  
  553.  
  554. output_base()
  555. {
  556.     register int i, j;
  557.  
  558.     fprintf(output_file, "short yysindex[] = {%39d,", base[0]);
  559.  
  560.     j = 10;
  561.     for (i = 1; i < nstates; i++)
  562.     {
  563.     if (j >= 10)
  564.     {
  565.         ++outline;
  566.         putc('\n', output_file);
  567.         j = 1;
  568.     }
  569.     else
  570.         ++j;
  571.  
  572.     fprintf(output_file, "%5d,", base[i]);
  573.     }
  574.  
  575.     outline += 2;
  576.     fprintf(output_file, "\n};\nshort yyrindex[] = {%39d,",
  577.         base[nstates]);
  578.  
  579.     j = 10;
  580.     for (i = nstates + 1; i < 2*nstates; i++)
  581.     {
  582.     if (j >= 10)
  583.     {
  584.         ++outline;
  585.         putc('\n', output_file);
  586.         j = 1;
  587.     }
  588.     else
  589.         ++j;
  590.  
  591.     fprintf(output_file, "%5d,", base[i]);
  592.     }
  593.  
  594.     outline += 2;
  595.     fprintf(output_file, "\n};\nshort yygindex[] = {%39d,",
  596.         base[2*nstates]);
  597.  
  598.     j = 10;
  599.     for (i = 2*nstates + 1; i < nvectors - 1; i++)
  600.     {
  601.     if (j >= 10)
  602.     {
  603.         ++outline;
  604.         putc('\n', output_file);
  605.         j = 1;
  606.     }
  607.     else
  608.         ++j;
  609.  
  610.     fprintf(output_file, "%5d,", base[i]);
  611.     }
  612.  
  613.     outline += 2;
  614.     fprintf(output_file, "\n};\n");
  615.     FREE(base);
  616. }
  617.  
  618.  
  619.  
  620. output_table()
  621. {
  622.     register int i;
  623.     register int j;
  624.  
  625.     ++outline;
  626.     fprintf(output_file, "#define YYTABLESIZE %d\n", high);
  627.     fprintf(output_file, "short yytable[] = {%40d,", table[0]);
  628.  
  629.     j = 10;
  630.     for (i = 1; i <= high; i++)
  631.     {
  632.     if (j >= 10)
  633.     {
  634.         ++outline;
  635.         putc('\n', output_file);
  636.         j = 1;
  637.     }
  638.     else
  639.         ++j;
  640.  
  641.     fprintf(output_file, "%5d,", table[i]);
  642.     }
  643.  
  644.     outline += 2;
  645.     fprintf(output_file, "\n};\n");
  646.     FREE(table);
  647. }
  648.  
  649.  
  650.  
  651. output_check()
  652. {
  653.     register int i;
  654.     register int j;
  655.  
  656.     fprintf(output_file, "short yycheck[] = {%40d,", check[0]);
  657.  
  658.     j = 10;
  659.     for (i = 1; i <= high; i++)
  660.     {
  661.     if (j >= 10)
  662.     {
  663.         ++outline;
  664.         putc('\n', output_file);
  665.         j = 1;
  666.     }
  667.     else
  668.         ++j;
  669.  
  670.     fprintf(output_file, "%5d,", check[i]);
  671.     }
  672.  
  673.     outline += 2;
  674.     fprintf(output_file, "\n};\n");
  675.     FREE(check);
  676. }
  677.  
  678.  
  679. int
  680. is_C_identifier(name)
  681. char *name;
  682. {
  683.     register char *s;
  684.     register int c;
  685.  
  686.     s = name;
  687.     c = *s;
  688.     if (c == '"')
  689.     {
  690.     c = *++s;
  691.     if (!isalpha(c) && c != '_' && c != '$')
  692.         return (0);
  693.     while ((c = *++s) != '"')
  694.     {
  695.         if (!isalnum(c) && c != '_' && c != '$')
  696.         return (0);
  697.     }
  698.     return (1);
  699.     }
  700.  
  701.     if (!isalpha(c) && c != '_' && c != '$')
  702.     return (0);
  703.     while (c = *++s)
  704.     {
  705.     if (!isalnum(c) && c != '_' && c != '$')
  706.         return (0);
  707.     }
  708.     return (1);
  709. }
  710.  
  711.  
  712. output_defines()
  713. {
  714.     register int c, i;
  715.     register char *s;
  716.  
  717.     for (i = 2; i < ntokens; ++i)
  718.     {
  719.     s = symbol_name[i];
  720.     if (is_C_identifier(s))
  721.     {
  722.         fprintf(output_file, "#define ");
  723.         if (dflag) fprintf(defines_file, "#define ");
  724.         c = *s;
  725.         if (c == '"')
  726.         {
  727.         while ((c = *++s) != '"')
  728.         {
  729.             putc(c, output_file);
  730.             if (dflag) putc(c, defines_file);
  731.         }
  732.         }
  733.         else
  734.         {
  735.         do
  736.         {
  737.             putc(c, output_file);
  738.             if (dflag) putc(c, defines_file);
  739.         }
  740.         while (c = *++s);
  741.         }
  742.         ++outline;
  743.         fprintf(output_file, " %d\n", symbol_value[i]);
  744.         if (dflag) fprintf(defines_file, " %d\n", symbol_value[i]);
  745.     }
  746.     }
  747.  
  748.     ++outline;
  749.     fprintf(output_file, "#define YYERRCODE %d\n", symbol_value[1]);
  750.  
  751.     if (dflag && unionized)
  752.     {
  753.     fclose(union_file);
  754.     union_file = fopen(union_file_name, "r");
  755.     if (union_file == NULL) open_error(union_file_name);
  756.     while ((c = getc(union_file)) != EOF)
  757.         putc(c, defines_file);
  758.     fprintf(defines_file, " YYSTYPE;\nextern YYSTYPE yylval;\n");
  759.     }
  760. }
  761.  
  762.  
  763. output_stored_text()
  764. {
  765.     register int c;
  766.     register FILE *in, *out;
  767.  
  768.     fclose(text_file);
  769.     text_file = fopen(text_file_name, "r");
  770.     if (text_file == NULL) open_error(text_file_name);
  771.     in = text_file;
  772.     out = output_file;
  773.     if ((c = getc(in)) == EOF)
  774.     return;
  775.     if (c == '\n') ++outline;
  776.     putc(c, out);
  777.     while ((c = getc(in)) != EOF)
  778.     {
  779.     if (c == '\n') ++outline;
  780.     putc(c, out);
  781.     }
  782.     if (!lflag)
  783.     {
  784.     ++outline;
  785.     fprintf(out, line_format, outline + 1, output_file_name);
  786.     }
  787. }
  788.  
  789.  
  790. output_debug()
  791. {
  792.     register int i, j, k, max;
  793.     char **symnam, *s;
  794.  
  795.     ++outline;
  796.     fprintf(output_file, "#define YYFINAL %d\n", final_state);
  797.     outline += 3;
  798.     fprintf(output_file, "#ifndef YYDEBUG\n#define YYDEBUG %d\n#endif\n",
  799.         tflag);
  800.  
  801.     max = 0;
  802.     for (i = 2; i < ntokens; ++i)
  803.     if (symbol_value[i] > max)
  804.         max = symbol_value[i];
  805.     ++outline;
  806.     fprintf(output_file, "#define YYMAXTOKEN %d\n", max);
  807.  
  808.     symnam = (char **) MALLOC((max+1)*sizeof(char *));
  809.     if (symnam == 0) no_space();
  810.  
  811.     /* Note that it is  not necessary to initialize the element        */
  812.     /* symnam[max].                            */
  813.     for (i = 0; i < max; ++i)
  814.     symnam[i] = 0;
  815.     for (i = ntokens - 1; i >= 2; --i)
  816.     symnam[symbol_value[i]] = symbol_name[i];
  817.     symnam[0] = "end-of-file";
  818.  
  819.     ++outline;
  820.     fprintf(output_file, "#if YYDEBUG\nchar *yyname[] = {");
  821.     j = 80;
  822.     for (i = 0; i <= max; ++i)
  823.     {
  824.     if (s = symnam[i])
  825.     {
  826.         if (s[0] == '"')
  827.         {
  828.         k = 7;
  829.         while (*++s != '"')
  830.         {
  831.             ++k;
  832.             if (*s == '\\')
  833.             {
  834.             k += 2;
  835.             if (*++s == '\\')
  836.                 ++k;
  837.             }
  838.         }
  839.         j += k;
  840.         if (j > 80)
  841.         {
  842.             ++outline;
  843.             putc('\n', output_file);
  844.             j = k;
  845.         }
  846.         fprintf(output_file, "\"\\\"");
  847.         s = symnam[i];
  848.         while (*++s != '"')
  849.         {
  850.             if (*s == '\\')
  851.             {
  852.             fprintf(output_file, "\\\\");
  853.             if (*++s == '\\')
  854.                 fprintf(output_file, "\\\\");
  855.             else
  856.                 putc(*s, output_file);
  857.             }
  858.             else
  859.             putc(*s, output_file);
  860.         }
  861.         fprintf(output_file, "\\\"\",");
  862.         }
  863.         else if (s[0] == '\'')
  864.         {
  865. fprintf(stderr, "s[0] = %c, s[1] = %c, s[2] = %c\n", s[0], s[1], s[2]);
  866.         if (s[1] == '"')
  867.         {
  868.             j += 7;
  869.             if (j > 80)
  870.             {
  871.             ++outline;
  872.             putc('\n', output_file);
  873.             j = 7;
  874.             }
  875.             fprintf(output_file, "\"'\\\"'\",");
  876.         }
  877.         else
  878.         {
  879.             k = 5;
  880.             while (*++s != '\'')
  881.             {
  882. fprintf(stderr, "k = %d, *s = %c\n", k, *s);
  883.             ++k;
  884.             if (*s == '\\')
  885.             {
  886.                 k += 2;
  887.                 if (*++s == '\\')
  888.                 ++k;
  889.             }
  890. fprintf(stderr, "k = %d, *s = %c\n", k, *s);
  891.             }
  892.             j += k;
  893.             if (j > 80)
  894.             {
  895.             ++outline;
  896.             putc('\n', output_file);
  897.             j = k;
  898.             }
  899.             fprintf(output_file, "\"'");
  900.             s = symnam[i];
  901.             while (*++s != '\'')
  902.             {
  903.             if (*s == '\\')
  904.             {
  905.                 fprintf(output_file, "\\\\");
  906.                 if (*++s == '\\')
  907.                 fprintf(output_file, "\\\\");
  908.                 else
  909.                 putc(*s, output_file);
  910.             }
  911.             else
  912.                 putc(*s, output_file);
  913.             }
  914.             fprintf(output_file, "'\",");
  915.         }
  916.         }
  917.         else
  918.         {
  919.         k = strlen(s) + 3;
  920.         j += k;
  921.         if (j > 80)
  922.         {
  923.             ++outline;
  924.             putc('\n', output_file);
  925.             j = k;
  926.         }
  927.         putc('"', output_file);
  928.         do { putc(*s, output_file); } while (*++s);
  929.         fprintf(output_file, "\",");
  930.         }
  931.     }
  932.     else
  933.     {
  934.         j += 2;
  935.         if (j > 80)
  936.         {
  937.         ++outline;
  938.         putc('\n', output_file);
  939.         j = 2;
  940.         }
  941.         fprintf(output_file, "0,");
  942.     }
  943.     }
  944.     outline += 2;
  945.     fprintf(output_file, "\n};\n");
  946.     FREE(symnam);
  947.  
  948.     ++outline;
  949.     fprintf(output_file, "char *yyrule[] = {\n");
  950.     for (i = 2; i < nrules; ++i)
  951.     {
  952.     fprintf(output_file, "\"%s :", symbol_name[rlhs[i]]);
  953.     for (j = rrhs[i]; ritem[j] > 0; ++j)
  954.     {
  955.         s = symbol_name[ritem[j]];
  956.         if (s[0] == '"')
  957.         {
  958.         fprintf(output_file, " \\\"");
  959.         while (*++s != '"')
  960.         {
  961.             if (*s == '\\')
  962.             {
  963.             if (s[1] == '\\')
  964.                 fprintf(output_file, "\\\\\\\\");
  965.             else
  966.                 fprintf(output_file, "\\\\%c", s[1]);
  967.             ++s;
  968.             }
  969.             else
  970.             putc(*s, output_file);
  971.         }
  972.         fprintf(output_file, "\\\"");
  973.         }
  974.         else if (s[0] == '\'')
  975.         {
  976.         if (s[1] == '"')
  977.             fprintf(output_file, " '\\\"'");
  978.         else if (s[1] == '\\')
  979.         {
  980.             if (s[2] == '\\')
  981.             fprintf(output_file, " '\\\\\\\\");
  982.             else
  983.             fprintf(output_file, " '\\\\%c", s[2]);
  984.             s += 2;
  985.             while (*++s != '\'')
  986.             putc(*s, output_file);
  987.             putc('\'', output_file);
  988.         }
  989.         else
  990.             fprintf(output_file, " '%c'", s[1]);
  991.         }
  992.         else
  993.         fprintf(output_file, " %s", s);
  994.     }
  995.     ++outline;
  996.     fprintf(output_file, "\",\n");
  997.     }
  998.  
  999.     outline += 2;
  1000.     fprintf(output_file, "};\n#endif\n");
  1001. }
  1002.  
  1003.  
  1004. output_stype()
  1005. {
  1006.     if (!unionized && ntags == 0)
  1007.     {
  1008.     outline += 3;
  1009.     fprintf(output_file, "#ifndef YYSTYPE\ntypedef int YYSTYPE;\n#endif\n");
  1010.     }
  1011. }
  1012.  
  1013.  
  1014. output_trailing_text()
  1015. {
  1016.     register int c, last;
  1017.  
  1018.     if (line == 0)
  1019.     return;
  1020.  
  1021.     c = *cptr;
  1022.     if (c == '\n')
  1023.     {
  1024.     ++lineno;
  1025.     if ((c = getc(input_file)) == EOF)
  1026.         return;
  1027.     if (!lflag)
  1028.     {
  1029.         ++outline;
  1030.         fprintf(output_file, line_format, lineno, input_file_name);
  1031.     }
  1032.     if (c == '\n') ++outline;
  1033.     putc(c, output_file);
  1034.     last = c;
  1035.     }
  1036.     else
  1037.     {
  1038.     if (!lflag)
  1039.     {
  1040.         ++outline;
  1041.         fprintf(output_file, line_format, lineno, input_file_name);
  1042.     }
  1043.     do { putc(c, output_file); } while ((c = *++cptr) != '\n');
  1044.     ++outline;
  1045.     putc('\n', output_file);
  1046.     last = '\n';
  1047.     }
  1048.  
  1049.     while ((c = getc(input_file)) != EOF)
  1050.     {
  1051.     if (c == '\n') ++outline;
  1052.     putc(c, output_file);
  1053.     last = c;
  1054.     }
  1055.  
  1056.     if (last != '\n')
  1057.     {
  1058.     ++outline;
  1059.     putc('\n', output_file);
  1060.     }
  1061.     if (!lflag)
  1062.     {
  1063.     ++outline;
  1064.     fprintf(output_file, line_format, outline + 1, output_file_name);
  1065.     }
  1066. }
  1067.  
  1068.  
  1069. output_semantic_actions()
  1070. {
  1071.     register int c, last;
  1072.  
  1073.     fclose(action_file);
  1074.     action_file = fopen(action_file_name, "r");
  1075.     if (action_file == NULL) open_error(action_file_name);
  1076.  
  1077.     if ((c = getc(action_file)) == EOF)
  1078.     return;
  1079.     last = c;
  1080.     if (c == '\n') ++outline;
  1081.     putc(c, output_file);
  1082.     while ((c = getc(action_file)) != EOF)
  1083.     {
  1084.     if (c == '\n') ++outline;
  1085.     putc(c, output_file);
  1086.     last = c;
  1087.     }
  1088.  
  1089.     if (last != '\n')
  1090.     {
  1091.     ++outline;
  1092.     putc('\n', output_file);
  1093.     }
  1094.     if (!lflag)
  1095.     {
  1096.     ++outline;
  1097.     fprintf(output_file, line_format, outline + 1, output_file_name);
  1098.     }
  1099. }
  1100.  
  1101.  
  1102. free_itemsets()
  1103. {
  1104.     register core *cp, *next;
  1105.  
  1106.     FREE(state_table);
  1107.     for (cp = first_state; cp; cp = next)
  1108.     {
  1109.     next = cp->next;
  1110.     FREE(cp);
  1111.     }
  1112. }
  1113.  
  1114.  
  1115. free_shifts()
  1116. {
  1117.     register shifts *sp, *next;
  1118.  
  1119.     FREE(shift_table);
  1120.     for (sp = first_shift; sp; sp = next)
  1121.     {
  1122.     next = sp->next;
  1123.     FREE(sp);
  1124.     }
  1125. }
  1126.  
  1127.  
  1128.  
  1129. free_reductions()
  1130. {
  1131.     register reductions *rp, *next;
  1132.  
  1133.     FREE(reduction_table);
  1134.     for (rp = first_reduction; rp; rp = next)
  1135.     {
  1136.     next = rp->next;
  1137.     FREE(rp);
  1138.     }
  1139. }
  1140.